package com.itheima.ssm.controller;

import com.itheima.ssm.domain.Product;
import com.itheima.ssm.service.IProductService;
import com.itheima.ssm.service.impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.List;

@Controller
@RequestMapping(path = "/product")
public class ProductController {

    @Autowired
    private IProductService iProductService;

    //    添加产品
    @RequestMapping(path = "/save")
    public String save(Product product) throws Exception {
        iProductService.save(product);
        return "redirect:findall";
    }

    //    查询全部产品
    @RolesAllowed("ADMIN")
    @RequestMapping(value = "/findall")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Product> ps = iProductService.findAll();
        mv.addObject("productList", ps);
        mv.setViewName("product-list");
        return mv;

    }
}
